<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "order_info".
 *
 * @property int $id
 * @property int $user_id 用户id
 * @property string $order_sn 订单号
 * @property double $money 订单金额
 * @property int $pay_date 支付时间
 * @property int $state 订单状态
 * @property string $address_name 收货人姓名
 * @property string $address_phone 收货人联系方式
 * @property string $address 收货人地址
 * @property string $order_send_name 快递名称
 * @property string $order_send_code 快递单号
 * @property string $remark 订单备注
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $send_at 发货时间
 * @property int $receive_at 收货时间
 */
class OrderInfo extends Base
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'order_info';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['user_id', 'pay_date', 'state', 'status', 'created_at', 'updated_at', 'send_at', 'receive_at', 'finsh_at', 'cancel_at'], 'integer'],
            [['money', 'money_yf'], 'number'],
            [['remark', 'file'], 'string'],
            [['order_sn', 'address_name', 'address_phone', 'address', 'order_send_name', 'order_send_code', 'code', 'agreement_name', 'user_name'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'order_sn' => 'Order Sn',
            'money' => 'Money',
            'pay_date' => 'Pay Date',
            'state' => 'State',
            'address_name' => 'Address Name',
            'address_phone' => 'Address Phone',
            'address' => 'Address',
            'order_send_name' => 'Order Send Name',
            'order_send_code' => 'Order Send Code',
            'remark' => 'Remark',
            'status' => 'Status',
            'created_at' => 'Created At',
            'send_at' => 'Send At',
            'receive_at' => 'Receive At',
        ];
    }

    //生成订单码
    public static function generateCode() {
        $arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        $key = array_rand($arr); //array_rand() 函数返回数组中的随机键
        $code = $arr[$key];
        return $code;
    }

    public function getUser(){
        return $this->hasOne(User::class, ['id' => 'user_id']);
    }
}
